package com.jiezuo.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiezuo.auth.entity.PermissionApi;
import com.jiezuo.auth.entity.PermissionApiField;
import com.jiezuo.auth.mapper.PermissionApiMapper;
import com.jiezuo.auth.service.PermissionApiFieldService;
import com.jiezuo.auth.service.PermissionApiService;
import com.jiezuo.common.constant.SysConstant;
import com.jiezuo.common.util.VerifyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
* @description   数据权限表
* @author        庞留杰
* @date          2022/01/18 18:08
* @version       V1.0
**/
@Transactional
@Slf4j
@Service
public class PermissionApiServiceImpl extends ServiceImpl<PermissionApiMapper, PermissionApi> implements PermissionApiService{

    @Autowired
    StringRedisTemplate redisTemplate;
    @Autowired
    PermissionApiFieldService permissionApiFieldService;

    @Override
    public List<PermissionApi> findByPermissionId(String permission_id) {
        VerifyUtil.notEmpty("资源ID",permission_id);
        QueryWrapper<PermissionApi> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete","0");
        queryWrapper.eq("permission_id",permission_id);
        List<PermissionApi> list = this.list(queryWrapper);
        list.forEach(it -> {
            QueryWrapper<PermissionApiField> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("is_delete", "0");
            queryWrapper1.eq("api_id", it.getId());
            List<PermissionApiField> list1 = permissionApiFieldService.list(queryWrapper1);
            it.setFields(list1);
        });
        // 清理redis数据
        Set<String> keys = redisTemplate.keys(SysConstant.REDIS_DATA_AUTHORITY+"*");
        Long delete = redisTemplate.delete(keys);
        return list;
    }

    @Override
    public boolean edit(PermissionApi permissionApi) {
        boolean b = this.saveOrUpdate(permissionApi);
        List<PermissionApiField> fields = permissionApi.getFields();
        // 删除
        UpdateWrapper<PermissionApiField> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("is_delete", "0");
        updateWrapper.eq("api_id", permissionApi.getId());
        updateWrapper.set("is_delete", "1");
        boolean update = permissionApiFieldService.update(updateWrapper);
        // 新增
        fields.forEach(it->{
            it.setId(null);
            it.setApi_id(permissionApi.getId());
        });
        permissionApiFieldService.saveBatch(fields);
        // 清理redis数据
        Set<String> keys = redisTemplate.keys(SysConstant.REDIS_DATA_AUTHORITY+"*");
        Long delete = redisTemplate.delete(keys);
        return b;
    }

    @Override
    public boolean delete(String id) {
        PermissionApi permissionApi = new PermissionApi();
        permissionApi.setId(id);
        permissionApi.setIs_delete("1");
        boolean b = this.updateById(permissionApi);
        UpdateWrapper<PermissionApiField> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("is_delete", "0");
        updateWrapper.eq("api_id", id);
        updateWrapper.set("is_delete", "1");
        permissionApiFieldService.update(updateWrapper);
        // 清理redis数据
        Set<String> keys = redisTemplate.keys(SysConstant.REDIS_DATA_AUTHORITY+"*");
        Long delete = redisTemplate.delete(keys);
        return b;
    }
}
